package com.mall.user.services;

import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

@Service
@Component
@Slf4j
public class VerifyServiceImpl  implements IVerifyService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserVerifyMapper userVerifyMapper;


    /**
     *  用户注册激活
     * @param verifyRequest
     * @return
     */
    @Override
    public UserVerifyResponse userVerify(UserVerifyRequest verifyRequest) {
        UserVerifyResponse verifyResponse = new UserVerifyResponse();
        // 获取到了uid和username
        //1. 更改用户表中的is_verify字段值
        int updateMemberEffectRow = memberMapper.updateMemberIsVerified(verifyRequest.getUserName());
        // 2.更改用户验证表中的is_verify字段的值
        int updateVerifyEffectRow = userVerifyMapper.updateVerifyIsVerifyByUserName(verifyRequest.getUserName());
        if(updateMemberEffectRow != 1 || updateVerifyEffectRow != 1){
            // 更改表中的数据有任何一方有错，就抛出去
            log.error("该用户名未激活");
            verifyResponse.setCode(SysRetCodeConstants.DB_EXCEPTION.getCode());
            verifyResponse.setMsg(SysRetCodeConstants.DB_EXCEPTION.getMessage());
            return verifyResponse;
        }
        // 成功
        log.error("该用户名已激活");
        verifyResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        verifyResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return verifyResponse;
    }
}
